package com.whfc.ms.api.safety.service.impl;

import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.util.RandomUtil;
import com.whfc.ms.api.safety.service.MsSafetyDangerService;
import com.whfc.safety.dto.*;
import com.whfc.safety.param.SafetyDangerAddParam;
import com.whfc.safety.param.SafetyDangerAttachParam;
import com.whfc.safety.param.SafetyDangerAttachSaveParam;
import com.whfc.safety.param.SafetyDangerEditParam;
import com.whfc.safety.service.SafetyDangerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Collections;
import java.util.List;

/**
 * @Description 危大工程
 * @Author hw
 * @Date 2021-08-19 17:12
 * @Version 1.0
 */
@Service
public class MsSafetyDangerServiceImpl implements MsSafetyDangerService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = SafetyDangerService.class, version = "1.0.0")
    private SafetyDangerService safetyDangerService;

    @Autowired
    private FilePathConfig filePathConfig;

    @Autowired
    private FileHandler fileHandler;

    @Override
    public ListData<SafetyDangerDictDTO> dictList() {
        List<SafetyDangerDictDTO> list = safetyDangerService.dictList();
        return new ListData<>(list);
    }

    @Override
    public ListData<SafetyDangerDictControlDTO> dictControlList(Integer dictId) {
        List<SafetyDangerDictControlDTO> list = safetyDangerService.dictControlList(dictId);
        return new ListData<>(list);
    }

    @Override
    public DangerStatisticalDTO statistical(Integer deptId) {
        return safetyDangerService.dangerStatistical(deptId);
    }

    @Override
    public PageData<SafetyDangerDTO> list(Integer deptId, Integer pageNum, Integer pageSize) {
        return safetyDangerService.dangerList(deptId, null, pageNum, pageSize);
    }

    @Override
    public void exportDanger(Integer deptId) {
        safetyDangerService.exportDanger(deptId, null);
    }

    @Override
    public void addDanger(SafetyDangerAddParam param) {
        safetyDangerService.addDanger(param);
    }

    @Override
    public void editDanger(SafetyDangerEditParam param) {
        safetyDangerService.editDanger(param);
    }

    @Override
    public void delDanger(Integer dangerId) {
        safetyDangerService.delDanger(dangerId);
    }

    @Override
    public PageData<SafetyDangerExtDTO> dangerExt(Integer dangerId, Integer type, Integer pageNum, Integer pageSize) {
        return safetyDangerService.dangerExt(dangerId, type, pageNum, pageSize);
    }

    @Override
    public void dangerExtDel(Integer dangerId, Integer extId, Integer type) {
        safetyDangerService.dangerExtDel(dangerId, extId, type);
    }

    @Override
    public ListData<SafetyDangerAcceptDTO> dangerAttachList(Integer dangerId) {
        List<SafetyDangerAcceptDTO> list = safetyDangerService.dangerAttachList(dangerId);
        return new ListData<>(list);
    }

    @Override
    public void dangerAttachSave(SafetyDangerAttachSaveParam param) {
        safetyDangerService.dangerAttachSave(param);
    }

    @Override
    public PageData<SafetyCheckDTO> dangerCheckList(Integer dangerId, Integer pageNum, Integer pageSize) {
        return safetyDangerService.dangerCheckList(dangerId, pageNum, pageSize);
    }

    @Override
    public SafetyCheckDescDTO dangerCheckDesc(Integer checkId) {
        return safetyDangerService.dangerCheckDesc(checkId);
    }

    @Override
    public void dangerCheckDel(Integer dangerId, Integer checkId) {
        safetyDangerService.dangerCheckDel(dangerId, checkId);
    }

    @Override
    public PageData<SafetyDangerAcceptDTO> dangerAcceptList(Integer dangerId, Integer pageNum, Integer pageSize) {
        return safetyDangerService.dangerAcceptList(dangerId, pageNum, pageSize);
    }

    @Override
    public void dangerComplete(Integer dangerId) {
        safetyDangerService.dangerComplete(dangerId);
    }

    @Override
    public ListData<SafetyDangerControlDTO> dangerControlList(Integer dangerId) {
        List<SafetyDangerControlDTO> list = safetyDangerService.dangerControlList(dangerId);
        return new ListData<>(list);
    }

    @Override
    public void acceptUploadImg(MultipartFile file, SafetyDangerAttachSaveParam param, String name, Integer size) {
        String upload = "";
        try {
            String originalFileName = file.getOriginalFilename();
            String suffix = originalFileName.substring(originalFileName.lastIndexOf(".") + 1);
            String path = filePathConfig.getFilePath("safety/img", RandomUtil.getRandomFileName(), suffix);
            InputStream inputStream = file.getInputStream();
            upload = fileHandler.upload(path, inputStream);
            SafetyDangerAttachParam param1 = new SafetyDangerAttachParam();
            param1.setSize(size);
            param1.setName(name);
            param1.setType(8);
            param1.setUrl(upload);
            param.setAttachUrls(Collections.singletonList(param1));
            safetyDangerService.dangerAcceptSave(param);
        } catch (Exception e) {
            logger.error("验收表验收文件上传失败", e);
        }
    }

    @Override
    public void dangerAcceptDel(Integer attachId) {
        safetyDangerService.dangerAttachDel(attachId);
    }
}
